package WaitingRoom;



import ServerClient.Client;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JCheckBox;
import javax.swing.UIManager;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author TUAN ANH
 */
public class ClientWaiting extends javax.swing.JFrame {

    /**
     * Creates new form SeverWaiting
     */
    Client client,requestToServer;
    public ClientWaiting(Client c) {
        this.client = c;
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception ex){
        }
        initComponents();
        setVisible(true);
        try{
        this.severName.setText(client.receiveData());
        this.clientName.setText(client.receiveData());
        this.scoreOfServer.setText(client.receiveData());
        this.scoreOfClient.setText(client.receiveData());
        this.idCode.setText(client.receiveData());
        } catch(Exception ex){
            System.out.println(""+ex.toString());
        }
        
//        doAction();
        try{
        requestToServer = new Client("localhost",3457);
        requestToServer.initConnection();
        }catch(Exception ex){
            
        }
        doAction();
    }
    public void doAction(){
        try{
            Client start = new Client("localhost",1234);
            start.initConnection();
            start.receiveData();
            dispose();
            start.close();
            requestToServer.close();
        }
        catch(Exception ex){
            
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        idLabel = new javax.swing.JLabel();
        severName = new javax.swing.JLabel();
        vs = new javax.swing.JLabel();
        clientName = new javax.swing.JLabel();
        scoreOfServer = new javax.swing.JLabel();
        scoreOfClient = new javax.swing.JLabel();
        idCode = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        waitingServer = new javax.swing.JLabel();
        waitingClient = new javax.swing.JLabel();
        readyCheckbox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Client Waiting Room");

        idLabel.setText("ID :");

        severName.setText("demoPlayer1");

        vs.setText("VS");

        clientName.setText("demoPlayer2");

        scoreOfServer.setText("2");

        scoreOfClient.setText("1");

        jLabel8.setText("Please select \"I'm ready\"");

        waitingServer.setForeground(new java.awt.Color(0, 0, 153));

        waitingClient.setForeground(new java.awt.Color(204, 0, 0));

        readyCheckbox.setText("I'm ready");
        readyCheckbox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                readyCheckboxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(idLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(idCode, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(severName)
                        .addGap(53, 53, 53))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(scoreOfServer, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(vs)
                        .addGap(51, 51, 51)
                        .addComponent(clientName)
                        .addGap(59, 59, 59))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(scoreOfClient, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(79, 79, 79))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(127, 127, 127)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(waitingClient)
                            .addComponent(waitingServer)
                            .addComponent(jLabel8)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(139, 139, 139)
                        .addComponent(readyCheckbox)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(idCode, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(idLabel)
                        .addComponent(severName)
                        .addComponent(clientName)
                        .addComponent(vs)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(scoreOfServer)
                    .addComponent(scoreOfClient))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addComponent(readyCheckbox)
                .addGap(18, 18, 18)
                .addComponent(waitingServer)
                .addGap(18, 18, 18)
                .addComponent(waitingClient)
                .addGap(90, 90, 90))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void readyCheckboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readyCheckboxActionPerformed
        // TODO add your handling code here:
        try{
        if(readyCheckbox.isSelected()) requestToServer.sentData("I'm ready");
        else requestToServer.sentData("Cancel");
        }catch(Exception ex){}
    }//GEN-LAST:event_readyCheckboxActionPerformed

    /**
     * @param args the command line arguments
     */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel clientName;
    private javax.swing.JLabel idCode;
    private javax.swing.JLabel idLabel;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JCheckBox readyCheckbox;
    private javax.swing.JLabel scoreOfClient;
    private javax.swing.JLabel scoreOfServer;
    private javax.swing.JLabel severName;
    private javax.swing.JLabel vs;
    private javax.swing.JLabel waitingClient;
    private javax.swing.JLabel waitingServer;
    // End of variables declaration//GEN-END:variables
}
